import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useState } from "react";
import SecondaryLayout from "../../compoents/secondary-layout";
import classes from "./personalInformation.module.less";
import classNames from "classnames";
import CustomInput from "../../compoents/input";
import { useLockFn, useReactive } from "ahooks";
import Modal from "../../compoents/modal";
import { useClipboard } from "use-clipboard-copy";
import toast from "../../compoents/toast";
import { useDispatch, useSelector } from "react-redux";
import { matchTheme } from "../../utils";
import kingsInstance from "../../instance";
import { EVENT } from "../../enums";
import { addInviter } from "../../api/user";
import { logout } from "../../api/auth";
import useServices from "../../hooks/useRequest";
import { upload } from "../../utils/aliyun";
import { updateUserInfo } from "../../api/user";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../local";
import { compressAccurately } from "image-conversion";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var InputCode = function(props) {
    var _useState = _sliced_to_array(useState(""), 2), referralCode = _useState[0], setReferralCode = _useState[1];
    var theme = useSelector(function(state) {
        return state.app.theme;
    });
    var dispatch = useDispatch();
    var services = useServices({
        addInviter: addInviter
    });
    var t = useTranslation().t;
    var save = useLockFn(/*#__PURE__*/ _async_to_generator(function() {
        return _ts_generator(this, function(_state) {
            if (!referralCode) return [
                2
            ];
            services.addInviter(referralCode).then(function(res) {
                if (res.code == 200) {
                    dispatch.app.setInviter().then(function() {
                        var _props_onClose;
                        setReferralCode("");
                        (_props_onClose = props.onClose) === null || _props_onClose === void 0 ? void 0 : _props_onClose.call(props);
                    });
                } else {
                    toast.createToast({
                        message: res.msg,
                        type: "errToast"
                    });
                }
            });
            return [
                2
            ];
        });
    }));
    return /*#__PURE__*/ _jsxs("div", {
        className: classes.input_modal,
        children: [
            /*#__PURE__*/ _jsxs("div", {
                className: classes.head,
                children: [
                    /*#__PURE__*/ _jsx("span", {
                        children: t(LocalKeyMap.referralCode)
                    }),
                    /*#__PURE__*/ _jsx("img", {
                        onClick: props.onClose,
                        src: matchTheme(theme, {
                            light: require("../../assets/icons/light/cancel.svg"),
                            dark: require("../../assets/icons/dark/cancel.svg")
                        }),
                        className: classes.close
                    })
                ]
            }),
            /*#__PURE__*/ _jsxs("div", {
                className: classes.body,
                children: [
                    /*#__PURE__*/ _jsx(CustomInput, {
                        value: referralCode,
                        onChange: setReferralCode,
                        className: classes.code_input,
                        maxLength: 10,
                        showMaxLength: true,
                        placeholder: t(LocalKeyMap["invitation.placeholder"])
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        onClick: save,
                        className: classNames(classes.save, referralCode ? "" : classes.save_disabled),
                        children: /*#__PURE__*/ _jsx("span", {
                            children: t(LocalKeyMap.save)
                        })
                    })
                ]
            })
        ]
    });
};
var ReferralInfo = function(props) {
    var clipboard = useClipboard({
        onSuccess: function onSuccess() {
            toast.copy();
        },
        onError: function onError() {
            console.log("复制失败");
        }
    });
    var t = useTranslation().t;
    var copy = function(content) {
        clipboard.copy(content);
    };
    return /*#__PURE__*/ _jsxs("div", {
        className: classes.referral_info,
        children: [
            /*#__PURE__*/ _jsxs("div", {
                children: [
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.title,
                        children: t(LocalKeyMap["personalInformation.referralTitle"])
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.desc,
                        children: t(LocalKeyMap["personalInformation.referralDesc"])
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.code,
                        children: [
                            /*#__PURE__*/ _jsx("span", {
                                children: props.userInfo.did
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.copy,
                                onClick: function() {
                                    copy(props.userInfo.did);
                                },
                                children: /*#__PURE__*/ _jsx("img", {
                                    src: matchTheme(props.theme, {
                                        light: require("../../assets/icons/copy.svg"),
                                        dark: require("../../assets/icons/dark/copy.svg")
                                    })
                                })
                            })
                        ]
                    })
                ]
            }),
            /*#__PURE__*/ _jsx("div", {
                className: classes.split_line,
                children: /*#__PURE__*/ _jsx("span", {
                    className: classes.text,
                    children: "or"
                })
            }),
            /*#__PURE__*/ _jsxs("div", {
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.link_box,
                        children: [
                            /*#__PURE__*/ _jsx("span", {
                                className: classes.link,
                                children: "http://links......social"
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.copy,
                                onClick: function() {
                                    copy(">http://links......social");
                                },
                                children: /*#__PURE__*/ _jsx("img", {
                                    src: matchTheme(props.theme, {
                                        light: require("../../assets/icons/copy.svg"),
                                        dark: require("../../assets/icons/dark/copy.svg")
                                    })
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.tip,
                        children: t(LocalKeyMap["personalInformation.invitation"])
                    })
                ]
            })
        ]
    });
};
function PersonalInformation() {
    var dispatch = useDispatch();
    var _useSelector = useSelector(function(state) {
        return {
            userInfo: state.app.userInfo,
            theme: state.app.theme,
            inviter: state.app.inviter,
            accountInfo: state.app.accountInfo
        };
    }), userInfo = _useSelector.userInfo, theme = _useSelector.theme, _useSelector_inviter = _useSelector.inviter, inviter = _useSelector_inviter === void 0 ? {} : _useSelector_inviter, accountInfo = _useSelector.accountInfo;
    var state = useReactive({
        /** referral code modal  */ referralCodeOpen: false,
        showReferralCode: false
    });
    var t = useTranslation().t;
    var services = useServices({
        logout: logout,
        upload: function() {
            var _ref = _async_to_generator(function(files) {
                var res;
                return _ts_generator(this, function(_state) {
                    switch(_state.label){
                        case 0:
                            return [
                                4,
                                upload(files)
                            ];
                        case 1:
                            res = _state.sent();
                            return [
                                2,
                                res
                            ];
                    }
                });
            });
            return function(files) {
                return _ref.apply(this, arguments);
            };
        }(),
        updateUserInfo: updateUserInfo
    });
    var signOut = function() {
        dispatch.app.deleteUserInfo();
        dispatch.app.PUSH("/login");
        kingsInstance.emit(EVENT.close);
    };
    var deleteAccount = function() {
        services.logout().then(function(res) {
            if ((res === null || res === void 0 ? void 0 : res.code) == 200) {
                signOut();
                toast.createToast({
                    message: t(LocalKeyMap["toast.closeSuccess"])
                });
            } else {
                toast.createToast({
                    type: "errToast",
                    message: "".concat(t(LocalKeyMap.fail), " !")
                });
            }
        }).catch(function(err) {
            toast.createToast({
                type: "errToast",
                message: err.msg
            });
        });
    };
    var signOutClick = function() {
        // setOpen(false);
        var destroy = toast.confirm({
            title: /*#__PURE__*/ _jsx("div", {
                children: t(LocalKeyMap["toast.confirm.signOut"])
            }),
            onConfirm: function() {
                destroy();
                signOut();
            }
        }).destroy;
    };
    var deleteAccountClick = function() {
        var text = t(LocalKeyMap["toast.confirm.deleteAccount"]);
        // if (accountInfo.totalActualAmount != '0') {
        //   text = `The account have some assets
        //   Are you sure to close account?
        //   The assets will be discarded.`;
        // }
        var destroy = toast.confirm({
            title: /*#__PURE__*/ _jsx("div", {
                children: text
            }),
            onConfirm: function() {
                destroy();
                deleteAccount();
            },
            className: classes.close_tip
        }).destroy;
    };
    var clipboard = useClipboard({
        onSuccess: function onSuccess() {
            toast.copy();
        },
        onError: function onError() {
            console.log("复制失败");
        }
    });
    var copy = function(content) {
        clipboard.copy(content);
    };
    var updateAvatar = function() {
        var _ref = _async_to_generator(function(e) {
            var files, _file;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        files = e.target.files;
                        if (!(files === null || files === void 0 ? void 0 : files.length)) return [
                            2
                        ];
                        _file = files[0];
                        if (!(files[0].size > 60000 * 1.25)) return [
                            3,
                            2
                        ];
                        return [
                            4,
                            compressAccurately(_file, 60)
                        ];
                    case 1:
                        // 雅座图片到指定大小左右
                        _file = _state.sent();
                        _state.label = 2;
                    case 2:
                        services.upload([
                            new File([
                                _file
                            ], files[0].name, {
                                type: _file.type
                            })
                        ]).then(function(urls) {
                            var avatarLink = urls[0];
                            /** 发送后端 */ services.updateUserInfo({
                                avatarLink: avatarLink
                            }).then(function(res) {
                                if (res.code == 200) {
                                    dispatch.app.SET_USER_INFO({
                                        avatarLink: avatarLink
                                    });
                                } else {
                                    toast.createToast({
                                        message: t(LocalKeyMap["toast.error.update"]),
                                        type: "errToast"
                                    });
                                }
                            }).catch(function(err) {
                                console.log(err);
                            });
                            e.target.files = null;
                        }).catch(function(err) {
                            console.log("err=>", err);
                        // e.target.files = null;
                        });
                        return [
                            2
                        ];
                }
            });
        });
        return function updateAvatar(e) {
            return _ref.apply(this, arguments);
        };
    }();
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            /*#__PURE__*/ _jsx(SecondaryLayout, {
                title: /*#__PURE__*/ _jsx(_Fragment, {
                    children: "\xa0"
                }),
                children: /*#__PURE__*/ _jsxs("div", {
                    className: classes.personal_information,
                    children: [
                        /*#__PURE__*/ _jsx("div", {
                            className: classes.personal_info,
                            children: /*#__PURE__*/ _jsxs("div", {
                                className: classes.avatar,
                                children: [
                                    /*#__PURE__*/ _jsxs("div", {
                                        className: classes.img,
                                        children: [
                                            /*#__PURE__*/ _jsx("img", {
                                                src: userInfo.avatarLink
                                            }),
                                            /*#__PURE__*/ _jsx("input", {
                                                onChange: updateAvatar,
                                                style: {
                                                    filter: "alpha(opacity=0)",
                                                    opacity: 0,
                                                    WebkitFilter: "alpha(opacity=0)"
                                                },
                                                className: classes.input,
                                                accept: "image/*",
                                                type: "file"
                                            })
                                        ]
                                    }),
                                    /*#__PURE__*/ _jsxs("div", {
                                        className: classes.name,
                                        children: [
                                            /*#__PURE__*/ _jsxs("div", {
                                                className: classes.nick,
                                                onClick: function() {
                                                    dispatch.app.PUSH("/setUsername");
                                                },
                                                children: [
                                                    userInfo.nickname,
                                                    /*#__PURE__*/ _jsx("img", {
                                                        src: matchTheme(theme, {
                                                            light: require("../../assets/icons/edit.svg"),
                                                            dark: require("../../assets/icons/edit.svg")
                                                        })
                                                    })
                                                ]
                                            }),
                                            /*#__PURE__*/ _jsxs("div", {
                                                className: classes.user_id,
                                                onClick: function() {
                                                    copy(userInfo.did);
                                                },
                                                children: [
                                                    "DID: ",
                                                    userInfo.did,
                                                    /*#__PURE__*/ _jsx("img", {
                                                        src: matchTheme(theme, {
                                                            light: require("../../assets/icons/copy.svg"),
                                                            dark: require("../../assets/icons/dark/copy.svg")
                                                        })
                                                    })
                                                ]
                                            })
                                        ]
                                    })
                                ]
                            })
                        }),
                        /*#__PURE__*/ _jsx("div", {
                            className: classes.personal_account,
                            children: /*#__PURE__*/ _jsxs("div", {
                                className: classes.account_list_warp,
                                children: [
                                    /*#__PURE__*/ _jsx("div", {
                                        onClick: function() {
                                            window.open("https://www.ona.social");
                                        },
                                        className: classNames(classes.sign_out, classes.account_list),
                                        children: /*#__PURE__*/ _jsxs("div", {
                                            children: [
                                                /*#__PURE__*/ _jsx("img", {
                                                    src: matchTheme(theme, {
                                                        light: require("../../assets/icons/light/aboutOna.svg"),
                                                        dark: require("../../assets/icons/light/aboutOna.svg")
                                                    }),
                                                    className: classes.list_icon
                                                }),
                                                /*#__PURE__*/ _jsx("span", {
                                                    children: t(LocalKeyMap.aboutOna)
                                                })
                                            ]
                                        })
                                    }),
                                    /*#__PURE__*/ _jsxs("div", {
                                        className: classNames(classes.account_bind, classes.account_list),
                                        onClick: function() {
                                            dispatch.app.PUSH("/accountBind");
                                        },
                                        children: [
                                            /*#__PURE__*/ _jsxs("div", {
                                                children: [
                                                    /*#__PURE__*/ _jsx("img", {
                                                        src: matchTheme(theme, {
                                                            light: require("../../assets/icons/light/bind.svg"),
                                                            dark: require("../../assets/icons/dark/bind.svg")
                                                        }),
                                                        className: classes.list_icon
                                                    }),
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: t(LocalKeyMap.accountBinding)
                                                    })
                                                ]
                                            }),
                                            /*#__PURE__*/ _jsx("img", {
                                                className: classes.arrow,
                                                src: matchTheme(theme, {
                                                    light: require("../../assets/icons/light/rightPArrow.svg"),
                                                    dark: require("../../assets/icons/dark/rightOLArrow.svg")
                                                })
                                            })
                                        ]
                                    }),
                                    /*#__PURE__*/ _jsxs("div", {
                                        onClick: function() {
                                            dispatch.app.PUSH("/myReferral");
                                        },
                                        className: classNames(classes.sign_out, classes.account_list),
                                        children: [
                                            /*#__PURE__*/ _jsxs("div", {
                                                children: [
                                                    /*#__PURE__*/ _jsx("img", {
                                                        src: matchTheme(theme, {
                                                            light: require("../../assets/icons/light/referrals.svg"),
                                                            dark: require("../../assets/icons/light/referrals.svg")
                                                        }),
                                                        className: classes.list_icon
                                                    }),
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: t(LocalKeyMap.myReferrals)
                                                    })
                                                ]
                                            }),
                                            /*#__PURE__*/ _jsx("img", {
                                                className: classes.arrow,
                                                src: matchTheme(theme, {
                                                    light: require("../../assets/icons/light/rightPArrow.svg"),
                                                    dark: require("../../assets/icons/dark/rightOLArrow.svg")
                                                })
                                            })
                                        ]
                                    }),
                                    /*#__PURE__*/ _jsx("div", {
                                        onClick: signOutClick,
                                        className: classNames(classes.sign_out, classes.account_list),
                                        children: /*#__PURE__*/ _jsxs("div", {
                                            children: [
                                                /*#__PURE__*/ _jsx("img", {
                                                    src: matchTheme(theme, {
                                                        light: require("../../assets/icons/light/out.svg"),
                                                        dark: require("../../assets/icons/dark/out.svg")
                                                    }),
                                                    className: classes.list_icon
                                                }),
                                                /*#__PURE__*/ _jsx("span", {
                                                    children: t(LocalKeyMap.signOut)
                                                })
                                            ]
                                        })
                                    })
                                ]
                            })
                        })
                    ]
                })
            }),
            /*#__PURE__*/ _jsx(Modal, {
                open: state.referralCodeOpen,
                contentClassName: classes.modal_center,
                children: /*#__PURE__*/ _jsx(InputCode, {
                    onClose: function() {
                        state.referralCodeOpen = false;
                    }
                })
            }),
            /*#__PURE__*/ _jsx(Modal, {
                onClose: function() {
                    state.showReferralCode = false;
                },
                open: state.showReferralCode,
                contentClassName: classes.referral_info_center,
                children: /*#__PURE__*/ _jsx(ReferralInfo, {
                    theme: theme,
                    userInfo: userInfo
                })
            })
        ]
    });
}
export default PersonalInformation;

 //# sourceMappingURL=index.js.map